package com.wuxiaoge.service;

import com.wuxiaoge.dao.StudentRepository;
import com.wuxiaoge.entity.Student;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class StudentService {

    private final StudentRepository studentRepository;

    // 查询数据库中指定名字的学生
    public List<Student> findByName(String name){
        List<Student> studentList = studentRepository.findByName(name);
        return studentList;
    };

    // 根据名字和年龄查询学生
    public List<Student> getByNameAndAge(String name, Integer age){
        List<Student> byNameAndAge = studentRepository.getByNameAndAge(name, age);
        return byNameAndAge;

    };

    //删除指定名字的学生,删除时需要在Service层调用是添加事务注解@Transactional
    @Transactional
    public Long deleteByName(String name){
        Long deleteByName = studentRepository.deleteByName(name);
        return deleteByName;
    };

    // 统计指定名字学生的数量
    public Long countByName(String name){
        Long countByName = studentRepository.countByName(name);
        return countByName;
    };


    public Student getOne(Long id){
        Student student = studentRepository.getOne(id);
        return student;
    };



}
